-- variables
local voicePlayers = {}

-- draw (voice reosource)
local g_screenX,g_screenY = guiGetScreenSize()
local BONE_ID = 8
local WORLD_OFFSET = 0.4
local ICON_PATH = "images/voice.png"
local ICON_WIDTH = 0.15*g_screenX
--
local iconHalfWidth = ICON_WIDTH/2

local ICON_DIMENSIONS = 12
local ICON_LINE = 20
local ICON_TEXT_SHADOW = tocolor ( 0, 0, 0, 255 )

playerrange = 20
--Draw the voice image
addEventHandler ( "onClientRender", root,
	function()
		local index = 0
		local sX, sY, sZ = getElementPosition(localPlayer) 
		for player in pairs(voicePlayers) do
            
			local rX, rY, rZ = getElementPosition(player)
			local distance = getDistanceBetweenPoints3D(sX, sY, sZ, rX, rY, rZ) 
			if getElementData(localPlayer,"player:kanal") == "2" or getElementData(localPlayer,"player:kanal") == "3" or getElementData(localPlayer,"player:kanal") == "4" or getElementData(localPlayer,"player:kanal") == "5" or getElementData(localPlayer,"player:kanal") == "6" then
		    playerrange = 9999
			setSoundVolume( player, 30 )
			else
		    playerrange = 20
			setSoundVolume( player, 30-distance )
			end

			local color = tocolor(255, 255, 255, 255)
			if distance <= playerrange then
			dxDrawVoiceLabel ( player, index, color )
			end
			index = index + 1
			while true do
				--is he streamed in?
				if not isElementStreamedIn(player) then
					break
				end
				--is he on screen?
				if not isElementOnScreen(player) then 
					break
				end
				local headX,headY,headZ = getPedBonePosition(player,BONE_ID)
				headZ = headZ + WORLD_OFFSET
				--is the head position on screen?
				local absX,absY = getScreenFromWorldPosition ( headX,headY,headZ )
				if not absX or not absY then
					break
				end
				local camX,camY,camZ = getCameraMatrix()
				--is there anything obstructing the icon?
				if not isLineOfSightClear ( camX, camY, camZ, headX, headY, headZ, true, false, false, true, false, true, false, player ) then
					break
				end
				dxDrawVoice ( absX, absY, color, getDistanceBetweenPoints3D(camX, camY, camZ, headX, headY, headZ) )
				break
			end
		end
	end
)

function dxDrawVoice ( posX, posY, color, distance )
	if distance <= playerrange then
	distance = 1/distance
	dxDrawImage ( posX - iconHalfWidth*distance, posY - iconHalfWidth*distance-75, ICON_WIDTH*distance, ICON_WIDTH*distance, ICON_PATH, 0, 0, 0, color, false )
end
end

loadstring(exports.dgs:dgsImportFunction())()
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )


function dxDrawVoiceLabel ( player, index, color )
	local sx, sy = guiGetScreenSize ()
	local scale = (sy / 800)
	local spacing = ( ICON_LINE * scale )
	local icon = ICON_DIMENSIONS * scale
	local px, py = sx-icon, (sy * 0.6 + spacing * index)

	if getPedOccupiedVehicle( localPlayer ) then
		py = (sy * 0.6 - spacing * index)
	end
	dxDrawImage ( px-icon, py, icon*1.2, icon*1.2, ICON_PATH, 0, 0, 0, color, false )

	px = px - spacing
		local name = getPlayerName ( player ):gsub("#%x%x%x%x%x%x","")
	local textWidth = dxGetTextWidth(name, 1.2, "default-bold")
	dxDrawText ( name, px - textWidth, py, px - textWidth, py, color, 1, font )
end


-- events
addEventHandler("onClientPlayerVoiceStart",getRootElement(),function()
	if not getElementData (source, "player:uid" ) then
		cancelEvent()
		return
	end
	if getElementData (source, "gracz:mute" ) then
		cancelEvent()
		return
	end
	if getElementData (source, "voice:off" ) then
		cancelEvent()
		return
	end
	local localDimension = getElementDimension( localPlayer )
	local localInterior = getElementInterior( localPlayer )
	if source == localPlayer then
		setElementData(source, "voice:mowi", true)

		-- draw
		voicePlayers[source] = true

		-- data to server
		local posX, posY, posZ = getElementPosition(source)
        local nearbyPlayers = getElementsWithinRange(posX, posY, posZ, 1, "player")
        -- senc data
        --triggerServerEvent( "voicechat:setDestination", resourceRoot, nearbyPlayers )
	else
		local sourceDimension = getElementDimension( source )
		local sourceInterior = getElementInterior( source )
		if tonumber(localDimension) ~= tonumber(sourceDimension) or tonumber(localInterior) ~= tonumber(sourceInterior) then
			cancelEvent()
			return
		end

		local sX, sY, sZ = getElementPosition(localPlayer) 
		local rX, rY, rZ = getElementPosition(source)
		local distance = getDistanceBetweenPoints3D(sX, sY, sZ, rX, rY, rZ) 
		if distance <= playerrange then
			voicePlayers[source] = true
		else 
			cancelEvent()
		end 
	end
end)

addEventHandler("onClientPlayerVoiceStop",getRootElement(),function()
	if source == localPlayer then
		setElementData(source, "voice:mowi", false)
	end
	voicePlayers[source] = nil
end)

addEventHandler("onClientPlayerQuit",getRootElement(),function()
	voicePlayers[source] = nil
end)